package org.info.stock.utils;

import com.alibaba.fastjson.JSONObject;
import org.info.stock.entity.LatestStocks;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.logging.Logger;

public class RestTemlpateUtils {

    private Logger logger = (Logger) LoggerFactory.getLogger(RestTemlpateUtils.class);

    public static JSONObject getForShObject(String url){
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        HttpEntity<String> httpEntity = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET,httpEntity,String.class);
        String responseBody = String.valueOf(responseEntity.getBody());
        String[] split = responseBody.split("\\(");
        String[] split2 = split[1].split("\\)");
        JSONObject jsonObject = JSONObject.parseObject(split2[0]);
        System.out.println(123);
        return jsonObject;
    }

    public static LatestStocks getForSzObject(String url){
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        HttpEntity<String> httpEntity = new HttpEntity<>(headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET,httpEntity,String.class);
        String responseBody = String.valueOf(responseEntity.getBody());
//        String[] split = responseBody.split("\\(");
//        String[] split2 = split[1].split("\\)");
        JSONObject jsonObject = JSONObject.parseObject(responseBody);
        String data = jsonObject.getString("data");
        LatestStocks latestStocks = JSONObject.parseObject(data, LatestStocks.class);
        System.out.println(123);
        return latestStocks;
    }

    public static void postForObject(String url){
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Accept", "application/json, text/javascript, */*; q=0.01");
        // 请求
        JSONObject requestMap = new JSONObject();
//        requestMap.put("cardid", item.getIdcard());
//        requestMap.put("name", item.getName());

        HttpEntity<JSONObject> entity = new HttpEntity<>(requestMap, headers);
        RestTemplate restTemplate = new RestTemplate();
//        String url ="https://qydzh.zwfw.guizhou.gov.cn/not-check/api/sjzb/gwyfpkfldxz/interface/pkryxxcx";
        JSONObject body1 = restTemplate.postForObject(url, entity, JSONObject.class);
        String response = body1.getString("response");
    }
}
